package com.android.hedgehog.puzzles;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.Assets;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;

public class GoalieScreen2 extends PuzzleScreen {
	enum GameState {
		Ready, Set1, Set2, Finish
	}

	public GoalieEngine2 field;
	Graphics graphics;
	int difficulty;
	GameState state;
	double resultTimer = 1.0f;
	String test = "";

	public GoalieScreen2(Game game, int gameType, int difficulty, int instNumber) {
		graphics = game.getGraphics();
		this.gameType = gameType;
		this.difficulty = difficulty;
		this.instanceNumber = instNumber;

		field = new GoalieEngine2(difficulty);
		state = GameState.Ready;
	}

	@Override
	public void update(List<TouchEvent> touchEvents, double deltaTime) {
		if (state == GameState.Ready)
			updateReady(touchEvents);
		if (state == GameState.Set1)
			updateSet1(touchEvents);
		if (state == GameState.Set2)
			updateSet2(deltaTime);
		if (state == GameState.Finish) {
			updateFinish(deltaTime);
		}

	}

	private void updateReady(List<TouchEvent> touchEvents) {
		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_DOWN || event.type == TouchEvent.TOUCH_DRAGGED) {
				if (event.x > field.block.x && event.x < field.block.x2 && event.y > field.block.y
						&& event.y < field.block.y2) {
					field.block.set(event.x, event.y);
				}

			}// DOWN && DRAGGED
			if (event.type == TouchEvent.TOUCH_UP) { // UP
				if (field.block.y > 400) {
					state = GameState.Set1;
				}

			}// UP
		}// FOR
	}

	private void updateSet1(List<TouchEvent> touchEvents) {
		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);

			if (event.type == TouchEvent.TOUCH_DOWN || event.type == TouchEvent.TOUCH_DRAGGED) {
				if (event.x > field.block.x && event.x < field.block.x2 && event.y > field.block.y
						&& event.y < field.block.y2) {
					field.block.set(event.x, event.y);
				}

			}// DOWN && DRAGGED

			if (event.type == TouchEvent.TOUCH_UP) { // UP

				if (field.block.y < 400) {
					state = GameState.Ready;
					return;
				}
				if (event.x > 200 && event.x < 260 && event.y > 340 && event.y < 380) {
					state = GameState.Set2;
					return;
				}

			}// UP

		} // FOR
	}

	private void updateSet2(double deltaTime) {
		field.puck.update((float) deltaTime);
		if (field.checkCollision()) {
			success = true;
			instanceScore = difficulty;
			state = GameState.Finish;
			return;
		}
		if (field.checkGoal()) {
			success = false;
			state = GameState.Finish;
			return;
		}
	}

	private void updateFinish(double deltaTime) {
		resultTimer -= deltaTime;
		if (resultTimer < 0)
			instanceOver = true;
	}

	@Override
	public void present() {
		graphics.drawUniversalRect(0, 31, 320, 459, false, true, 1, 255, Color.WHITE);

		graphics.drawLine(0, 31, 320, 31, Color.BLACK);
		graphics.drawLine(0, 400, 320, 400, Color.BLACK);
		graphics.drawUniversalRect(field.block.x, field.block.y, field.block.width, field.block.height,
				false, true, 1, 255, Color.BLUE);
		graphics.drawUniversalRect(field.block.x, field.block.y, field.block.width, field.block.height,
				false, false, 3, 255, Color.BLACK);

		graphics.drawPixmap(Assets.puck2, (int) field.puck.x, (int) field.puck.y);

		//graphics.drawText(field.testMsg, 170, 240, 12, Color.BLACK);// test

		if (state == GameState.Ready) {
			graphics.drawArrow(field.puck.x + 15, field.puck.y + 15, (field.puck.x + field.puck.dirX + 15),
					(field.puck.y + field.puck.dirY + 15), Color.RED);
			graphics.drawUniversalText("Place block behind goal line to defend", 1, true, 160, 320, 14, 255,
					Color.RED);
		}

		if (state == GameState.Set1) {
			graphics.drawUniversalRect(200, 340, 60, 40, false, true, 1, 255, Color.MAGENTA);
			graphics.drawUniversalRect(200, 340, 60, 40, false, false, 3, 255, Color.BLACK);
			graphics.drawUniversalText("SET", 1, true, 230, 367, 20, 255, Color.BLACK);
		}

		if (state == GameState.Finish) {

			if (success) {
				graphics.drawSpecialText("SUCCESS", 160, 200, 40, 2, Color.GREEN);
			} else {
				graphics.drawSpecialText("FAIL", 160, 200, 40, 2, Color.RED);
			}
			if (gameType == 1) {
				if (instanceScore > 0) {
					graphics.drawSpecialText("+" + instanceScore, 160, 240, 32, 2, Color.GREEN);
				}
			}

		}

	}

}
